package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.ReportLjlmPzdsmzcBaDetail;
import com.ruoyi.report_ljlm.domain.ReportLjlmPzdsmzcbgBsInfo;
import com.ruoyi.report_ljlm.domain.vo.ReportLjlmPzdsmzcBaDetailVo;
import com.ruoyi.report_ljlm.domain.vo.ReportLjlmPzdsmzcbgBsInfoVo;
import com.ruoyi.report_ljlm.mapper.ReportLjlmPzdsmzcBaDetailMapper;
import com.ruoyi.report_ys.domain.ReportYsDzkyqdsyBaDetail;
import com.ruoyi.report_ys.domain.vo.ReportYsDzkyqdsyBaDetailVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.ReportLjlmPzdsmzcBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.ReportLjlmPzdsmzcBsInfoVo;
import com.ruoyi.report_ljlm.domain.ReportLjlmPzdsmzcBsInfo;
import com.ruoyi.report_ljlm.mapper.ReportLjlmPzdsmzcBsInfoMapper;
import com.ruoyi.report_ljlm.service.IReportLjlmPzdsmzcBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路基路面平整度试验检测记录三米直尺法Service业务层处理
 *
 * @author zyb
 * @date 2024-10-29
 */
@RequiredArgsConstructor
@Service
@Transactional
public class ReportLjlmPzdsmzcBsInfoServiceImpl implements IReportLjlmPzdsmzcBsInfoService {

    private final ReportLjlmPzdsmzcBsInfoMapper baseMapper;
    private final ReportLjlmPzdsmzcBaDetailMapper detailMapper;

    /**
     * 查询路基路面平整度试验检测记录三米直尺法
     */
    @Override
    @FIllBasicInfo
    public ReportLjlmPzdsmzcBsInfoVo queryById(Long excelId) {
        ReportLjlmPzdsmzcBsInfoVo reportLjlmPzdsmzcBsInfoVo = baseMapper.selectVoById(excelId);
        List<ReportLjlmPzdsmzcBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<ReportLjlmPzdsmzcBaDetail>()
            .eq(ReportLjlmPzdsmzcBaDetail::getExcelId, excelId));
        if (CollectionUtils.isNotEmpty(baDetailVos)) {
            reportLjlmPzdsmzcBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return reportLjlmPzdsmzcBsInfoVo;
    }

    /**
     * 查询路基路面平整度试验检测记录三米直尺法列表
     */
    @Override
    public TableDataInfo<ReportLjlmPzdsmzcBsInfoVo> queryPageList(ReportLjlmPzdsmzcBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLjlmPzdsmzcBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLjlmPzdsmzcBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路基路面平整度试验检测记录三米直尺法列表
     */
    @Override
    public List<ReportLjlmPzdsmzcBsInfoVo> queryList(ReportLjlmPzdsmzcBsInfoBo bo) {
        LambdaQueryWrapper<ReportLjlmPzdsmzcBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLjlmPzdsmzcBsInfo> buildQueryWrapper(ReportLjlmPzdsmzcBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLjlmPzdsmzcBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLjlmPzdsmzcBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLjlmPzdsmzcBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLjlmPzdsmzcBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLjlmPzdsmzcBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLjlmPzdsmzcBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLjlmPzdsmzcBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLjlmPzdsmzcBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLjlmPzdsmzcBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLjlmPzdsmzcBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLjlmPzdsmzcBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLjlmPzdsmzcBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLjlmPzdsmzcBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLjlmPzdsmzcBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLjlmPzdsmzcBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLjlmPzdsmzcBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLjlmPzdsmzcBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLjlmPzdsmzcBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLjlmPzdsmzcBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLjlmPzdsmzcBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLjlmPzdsmzcBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLjlmPzdsmzcBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancld()), ReportLjlmPzdsmzcBsInfo::getJiancld, bo.getJiancld());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegcc()), ReportLjlmPzdsmzcBsInfo::getJiegcc, bo.getJiegcc());
        return lqw;
    }

    /**
     * 新增路基路面平整度试验检测记录三米直尺法
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportLjlmPzdsmzcBsInfoBo bo) {
        ReportLjlmPzdsmzcBsInfo add = BeanUtil.toBean(bo, ReportLjlmPzdsmzcBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<ReportLjlmPzdsmzcBaDetail> details = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                details.add(new ReportLjlmPzdsmzcBaDetail(bo.getExcelId(), Long.parseLong(String.valueOf(i))));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改路基路面平整度试验检测记录三米直尺法
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportLjlmPzdsmzcBsInfoBo bo) {
        ReportLjlmPzdsmzcBsInfo update = BeanUtil.toBean(bo, ReportLjlmPzdsmzcBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), ReportLjlmPzdsmzcBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLjlmPzdsmzcBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路基路面平整度试验检测记录三米直尺法
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
